﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SplashBehaviour.cs" company="http://buginbox.wordpress.com/">
//   Copyright (c) GNU General Public License version 2 (GPLv2)
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

using System.Windows;

namespace Module.Splash.Behaviour
{
    /// <summary>
    /// Splash Behaviour
    /// </summary>
    public class SplashBehaviour
    {
        #region Dependency Properties

        /// <summary>
        /// Enabled Property
        /// </summary>
        public static readonly DependencyProperty EnabledProperty = DependencyProperty.RegisterAttached("Enabled", typeof(bool), typeof(SplashBehaviour), new PropertyMetadata(OnEnabledChanged));

        /// <summary>
        /// Gets the enabled.
        /// </summary>
        /// <param name="obj">
        /// The obj. 
        /// </param>
        /// <returns>
        /// The get enabled. 
        /// </returns>
        public static bool GetEnabled(DependencyObject obj)
        {
            return (bool)obj.GetValue(EnabledProperty);
        }

        /// <summary>
        /// Sets the enabled.
        /// </summary>
        /// <param name="obj">
        /// The obj. 
        /// </param>
        /// <param name="value">
        /// if set to <c>true</c> [value]. 
        /// </param>
        public static void SetEnabled(DependencyObject obj, bool value)
        {
            obj.SetValue(EnabledProperty, value);
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Called when [enabled changed].
        /// </summary>
        /// <param name="obj">
        /// The obj. 
        /// </param>
        /// <param name="args">
        /// The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data. 
        /// </param>
        private static void OnEnabledChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            var splash = obj as Window;

            if (splash != null && args.NewValue is bool && (bool)args.NewValue)
            {
                splash.Closed += (s, e) =>
                    {
                        splash.DataContext = null;
                        splash.Dispatcher.InvokeShutdown();
                    };

                splash.MouseLeftButtonDown += (s, e) => splash.DragMove();

                ////splash.MouseDoubleClick += (s, e) => splash.Close();
            }
        }

        #endregion
    }
}